package cn.bdqn.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bdqn.pojo.Bill;
import cn.bdqn.pojo.Provider;
import cn.bdqn.pojo.User;
import cn.bdqn.service.BillService;
import cn.bdqn.service.ProviderService;
import cn.bdqn.util.PageBean;

@Controller
@RequestMapping("/bill")
public class BillController {
	
	@Autowired
	private BillService  billService;
	@Autowired
	private ProviderService providerService;
	
	@RequestMapping("/showPage")
	public String showPage(@RequestParam(value="pageNo",required=false,defaultValue="1")Integer pageNo,@RequestParam(value="queryProductName",required=false)String queryProductName
			,@RequestParam(value="queryProviderId",required=false)Integer queryProviderId
			,@RequestParam(value="queryIsPayment",required=false)Integer queryIsPayment,Model model){
		int pageSize=6;
		PageBean<Bill> bean=billService.findByPage(pageNo, pageSize, queryProductName, queryProviderId, queryIsPayment);
		List<Provider> list=providerService.findAll();
		model.addAttribute("queryProductName",queryProductName);
		model.addAttribute("queryProviderId",queryProviderId);
		model.addAttribute("queryIsPayment",queryIsPayment);
		model.addAttribute("providerList",list);
		model.addAttribute("pageBean",bean);
		return "billlist";
	}
	
	@ResponseBody
	@RequestMapping("/view/{billcc}")
	public Object view(@PathVariable("billcc") String billCode){
		Bill bill=billService.findByCode(billCode);
		return bill;
	}
	
	@RequestMapping("/billadd.html")
	public String billadd(){
		return "billadd";
	}
	
	@RequestMapping("/dobilladd")
	public String dobilladd(Bill bill,HttpSession session){
		bill.setCreatedBy(((User)session.getAttribute("userlogin")).getId());
		bill.setCreationDate(new Date());
		boolean isOk=billService.addBill(bill);
		if(isOk){
			return "billsuccess";
		}else{
			return "billadd";
		}
		
	}
	
	@ResponseBody
	@RequestMapping("/getproviderlist")
	public Object getProviderlist(){
		List<Provider> list=providerService.findAll();
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/checkBillCode")
	public Object checkBillCode(@RequestParam("billCode") String billCode){
		Map<String, Object> map=new HashMap<String, Object>();
		boolean isOk=billService.checkBillCode(billCode);
		if(isOk){
			map.put("billCode","exist");
		}else{
			map.put("billCode","noexist");
		}
		return map;
	}
	
	@RequestMapping("/modify/{billcc}")
	public String modify(@PathVariable("billcc")String billCode,Model model){
		Bill bill=billService.findByCode(billCode);
		model.addAttribute("bill",bill);
		return "billmodify";
	}
	
	@RequestMapping("/dobillmodify")
	public String dobillmodify(Bill bill,HttpSession session){
		bill.setModifyBy(((User)session.getAttribute("userlogin")).getId());
		bill.setModifyDate(new Date());
		boolean isOk=billService.updateByBill(bill);
		if(isOk){
			return "billsuccess";
		}else{
			return "billmodify";
		}
		
	}
	
	@ResponseBody
	@RequestMapping("/delete/{billcc}")
	public Object delete(@PathVariable("billcc")String billCode){
		Map<String, Object> map=new HashMap<String, Object>();
		if(billService.findByCode(billCode)!=null){
			int result=billService.deleteByCode(billCode);
			if(result>0){
				map.put("delResult","true");
			}else{
				map.put("delResult","false");
			}
		}else{
			map.put("delResult","notexist");
		}
		return map;
	}
}
